package com.bigkoo.svprogresshud.view;

import com.bigkoo.svprogresshud.ResourceTable;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.app.Context;

public class SVProgressDefaultView extends DirectionalLayout {

    private int resBigLoading = ResourceTable.Media_ic_svstatus_loading;
    private int resInfo = ResourceTable.Media_ic_svstatus_info;
    private int resSuccess = ResourceTable.Media_ic_svstatus_success;
    private int resError = ResourceTable.Media_ic_svstatus_error;
    private Image ivBigLoading;
    private Image ivSmallLoading;
    private SVCircleProgressBar circleProgressBar;
    private Text tvMsg;
    private boolean isBaseStatus = false;

    private AnimatorProperty mRotateAnimation;

    public SVProgressDefaultView(Context context) {
        super(context);
        initViews();
        init();
    }

    private void initViews() {
        ComponentContainer.LayoutConfig layoutConfig = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_view_svprogressdefault, this, true).getLayoutConfig();
        this.setLayoutConfig(layoutConfig);
        ivBigLoading = (Image) findComponentById(ResourceTable.Id_ivBigLoading);
        ivSmallLoading = (Image) findComponentById(ResourceTable.Id_ivSmallLoading);
        circleProgressBar = (SVCircleProgressBar) findComponentById(ResourceTable.Id_circleProgressBar);
        tvMsg = (Text) findComponentById(ResourceTable.Id_tvMsg);
    }

    private void init() {
        mRotateAnimation = new AnimatorProperty();
        mRotateAnimation.setCurveType(Animator.CurveType.LINEAR);
        mRotateAnimation.setDuration(1000L);
        mRotateAnimation.setLoopedCount(10000);
        mRotateAnimation.rotate(360f);
        mRotateAnimation.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                mRotateAnimation.start();
            }
        });
    }

    public void show() {
        mRotateAnimation.cancel();
        ivBigLoading.setPixelMap(resBigLoading);
        ivBigLoading.setVisibility(Component.VISIBLE);
        ivSmallLoading.setVisibility(Component.HIDE);
        circleProgressBar.setVisibility(Component.HIDE);
        tvMsg.setVisibility(Component.HIDE);
        //开启旋转动画
        mRotateAnimation.setTarget(ivBigLoading);
        ivBigLoading.setBindStateChangedListener(new Component.BindStateChangedListener(){
            @Override
            public void onComponentBoundToWindow(Component component) {
                mRotateAnimation.start();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
                mRotateAnimation.end();
            }
        });
    }

    public void showWithStatus(String string) {
        if (string == null) {
            show();
            return;
        }
        showBaseStatus(resBigLoading, string);
        isBaseStatus = true;
        //开启旋转动画
        mRotateAnimation.setTarget(ivSmallLoading);
        ivSmallLoading.setBindStateChangedListener(new Component.BindStateChangedListener(){
            @Override
            public void onComponentBoundToWindow(Component component) {
                if(isBaseStatus){
                    mRotateAnimation.start();
                    isBaseStatus = false;
                }
            }
            @Override
            public void onComponentUnboundFromWindow(Component component) {
                mRotateAnimation.end();
            }
        });
    }


    public void showInfoWithStatus(String string) {
        showBaseStatus(resInfo, string);
    }

    public void showSuccessWithStatus(String string) {
        showBaseStatus(resSuccess, string);
    }

    public void showErrorWithStatus(String string) {
        showBaseStatus(resError, string);
    }
    public void showWithProgress(String string) {
        showProgress(string);
    }

    public SVCircleProgressBar getCircleProgressBar() {
        return circleProgressBar;
    }

    public void setText(String string){
        tvMsg.setText(string);
    }


    public void showBaseStatus(int res, String string) {
        //mRotateAnimation.cancel();
        ivSmallLoading.setPixelMap(res);
        tvMsg.setText(string);
        ivBigLoading.setVisibility(Component.HIDE);
        circleProgressBar.setVisibility(Component.HIDE);
        ivSmallLoading.setVisibility(Component.VISIBLE);
        tvMsg.setVisibility(Component.VISIBLE);
    }

    public void showProgress(String string) {
        mRotateAnimation.cancel();
        tvMsg.setText(string);
        ivBigLoading.setVisibility(Component.HIDE);
        ivSmallLoading.setVisibility(Component.HIDE);
        circleProgressBar.setVisibility(Component.VISIBLE);
        tvMsg.setVisibility(Component.VISIBLE);
    }

    public void dismiss() {
        mRotateAnimation.cancel();
    }
}
